package cn.exrick.xboot.modules.activiti.serviceimpl.business;

import java.util.*;

import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.modules.activiti.vo.ActBusiMatter;
import cn.exrick.xboot.modules.base.entity.Department;
import cn.exrick.xboot.modules.base.entity.Role;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.base.service.DepartmentService;
import cn.exrick.xboot.modules.base.service.UserService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.activiti.dao.mapper.business.MatterAnalysisMapper;
import cn.exrick.xboot.modules.activiti.entity.business.MatterAnalysis;
import cn.exrick.xboot.modules.activiti.service.business.MatterAnalysisService;

@Service
public class MatterAnalysisServiceImpl implements MatterAnalysisService {
	@Autowired
	private SecurityUtil securityUtil;

	@Autowired
	private MatterAnalysisMapper matterAnalysisMapper;

	@Autowired
	private UserService userService;

	@Autowired
	private DepartmentService departmentService;

	@Override
	public IPage<MatterAnalysis> analysis1(IPage<MatterAnalysis> page, SearchVo searchVo, String deptId) {
		QueryWrapper<MatterAnalysis> wrapper = new QueryWrapper<MatterAnalysis>();
		//为解决以下列空字符串时报错，判断允许为NULL但不允许为空字符串
		wrapper.and(i -> i.isNull("t.field0012").or().gt("LENGTH(t.field0012)", 0));
		wrapper.and(i -> i.isNull("t.field0018").or().eq("t.field0018", 0));
//		wrapper.and(i -> i.isNull("t.field0041").or().gt("LENGTH(t.field0041)", 0));
//		wrapper.and(i -> i.isNull("t.field0042").or().gt("LENGTH(t.field0042)", 0));
		if (StringUtils.isNotEmpty(searchVo.getStartDate())) {
			wrapper.ge("t.field0002", searchVo.getStartDate());
		}
		if (StringUtils.isNotEmpty(searchVo.getEndDate())) {
			wrapper.le("t.field0010", searchVo.getEndDate());
		}
		if (StringUtils.isNotEmpty(deptId)) {
	        wrapper.eq("t.field0012", deptId);
		}
//		wrapper.ne("t.field0018","1");
		return matterAnalysisMapper.analysis1(page, wrapper);
	}

	@Override
	public IPage<MatterAnalysis> analysis2(IPage<MatterAnalysis> page, SearchVo searchVo, String deptId) {
		QueryWrapper<MatterAnalysis> wrapper = new QueryWrapper<MatterAnalysis>();
		//为解决以下列空字符串时报错，判断允许为NULL但不允许为空字符串
		wrapper.and(i -> i.isNull("t.field0012").or().gt("LENGTH(t.field0012)", 0));
		wrapper.and(i -> i.isNull("t.field0018").or().eq("t.field0018", 0));
//		wrapper.and(i -> i.isNull("t.field0041").or().gt("LENGTH(t.field0041)", 0));
//		wrapper.and(i -> i.isNull("t.field0042").or().gt("LENGTH(t.field0042)", 0));
		if (StringUtils.isNotEmpty(searchVo.getStartDate())) {
			wrapper.ge("t.field0002", searchVo.getStartDate());
		}
		if (StringUtils.isNotEmpty(searchVo.getEndDate())) {
			wrapper.le("t.field0010", searchVo.getEndDate());
		}
		if (StringUtils.isNotEmpty(deptId)) {
	        wrapper.eq("t.field0012", deptId);
		}
//		wrapper.ne("t.field0018","1");
		return matterAnalysisMapper.analysis2(page, wrapper);
	}

	@Override
	public IPage<MatterAnalysis> analysis3(IPage<MatterAnalysis> page, SearchVo searchVo, String typeId) {
		QueryWrapper<MatterAnalysis> wrapper = new QueryWrapper<MatterAnalysis>();
		//为解决以下列空字符串时报错，判断允许为NULL但不允许为空字符串
		wrapper.and(i -> i.isNull("field0007").or().gt("LENGTH(field0007)", 0));
		wrapper.and(i -> i.isNull("field0018").or().eq("field0018", 0));

		if (StringUtils.isNotEmpty(searchVo.getStartDate())) {
			wrapper.ge("field0002", searchVo.getStartDate());
		}
		if (StringUtils.isNotEmpty(searchVo.getEndDate())) {
			wrapper.le("field0010", searchVo.getEndDate());
		}
		if (StringUtils.isNotEmpty(typeId)) {
	        wrapper.eq("field0007", typeId);
		}
//		wrapper.ne("t.field0018","1");
		return matterAnalysisMapper.analysis3(page, wrapper);
	}

	@Override
	public IPage<ActBusiMatter> queryMatter(IPage<ActBusiMatter> page, SearchVo searchVo, ActBusiMatter matter) {
		QueryWrapper<ActBusiMatter> wrapper = new QueryWrapper<ActBusiMatter>();
		//为解决以下列空字符串时报错，判断允许为NULL但不允许为空字符串
//		wrapper.and(i -> i.isNull("field0007").or().gt("LENGTH(field0007)", 0));
		if (StrUtil.isNotBlank(matter.getTitle())) {
			wrapper.like("tm.field0005", '%' + matter.getTitle() + '%');
		}
		if (StrUtil.isNotBlank(matter.getField0003())) {
			wrapper.like("tm.field0003", '%' + matter.getField0003() + '%');
		}
		if (StrUtil.isNotBlank(matter.getField0004())) {
			wrapper.like("tm.field0004", '%' + matter.getField0004() + '%');
		}
		if (StrUtil.isNotBlank(matter.getField0006())) {
			wrapper.like("tm.field0006", '%' + matter.getField0006() + '%');
		}
		if (StrUtil.isNotBlank(matter.getWorkId())) {
			wrapper.like("tab.work_id", '%' + matter.getWorkId() + '%');
		}
		if(matter.getStatus()!=null){
			wrapper.eq("tab.status",matter.getStatus());
		}
		if(matter.getResult()!=null){
			wrapper.eq("tab.result",matter.getResult());
		}
		if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
			Date start = DateUtil.parse(searchVo.getStartDate());
			Date end = DateUtil.parse(searchVo.getEndDate());
			wrapper.between("tab.apply_time",start,DateUtil.endOfDay(end));
		}
		if(StrUtil.isNotBlank(searchVo.getCallStartDate())&&StrUtil.isNotBlank(searchVo.getCallEndDate())){
			Date start = DateUtil.parse(searchVo.getCallStartDate());
			Date end = DateUtil.parse(searchVo.getCallEndDate());
			wrapper.between("tm.field0002",start,DateUtil.endOfDay(end));
		}

		{
			// 用户换成部门所有人
			//获取所辖地区部门
			List<String> allRolesName = new ArrayList<>();
			List<String> allRolesFullName = new ArrayList<>();
			List<String> allDepartIds = new ArrayList<>();
			boolean isAll = false;
			List<String> showAllRoles = Arrays.asList("ROLE_网格员","ROLE_镇长","ROLE_书记","ROLE_监察员");
			for (Role r : securityUtil.getCurrUser().getRoles()) {
				if (showAllRoles.contains(r.getName())){
					//特殊权限显示所有
					isAll = true;
					break;
				}
				String[] spliceItems = r.getName().split("_");
				if (spliceItems.length == 3) {
					allRolesName.add(spliceItems[1]);
				}
			}
			List<Department> allDepart = departmentService.findByTitleInOrderBySortOrder(allRolesName);
			for (Department d : allDepart) {
				allDepartIds.add(d.getId());
			}
//			allDepartIds.add(securityUtil.getCurrUser().getDepartmentId());
			//承办部门数据
			if (!isAll && allDepartIds.size() > 0){
				wrapper.in("tm.field0012",allDepartIds);
			}
		}
		return matterAnalysisMapper.queryMatter(page, wrapper);
	}

	@Override
	public List<Map<String, String>> getAllMatterType() {
		return matterAnalysisMapper.findMatterType();
	}

}
